package com.zhang.boot.core.service.impl;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
@RabbitListener(queues = "hello-java-queue")
public class RabbitService {

    /**
     * 接收map消息
     *
     * @param message
     * @param map
     */
    @RabbitHandler
    public void receiveMap(Message message, Map<Object, Object> map, Channel channel) {
        log.info("接收到mq消息:{},消息内容:{}", message, map);
        //通道自增的
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        //ack 非批量ack
        try {
            channel.basicAck(deliveryTag, false);
            //拒绝ack
            //channel.basicNack(deliveryTag,false, true);
            //拒绝ack
            //channel.basicReject(deliveryTag, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
